home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / MacHaskell 2.2 / progs / demo / add.hs next >
Encoding:
Text File  |  1994-09-27  |  530 b   |  17 lines  |  [TEXT/YHS2]

  1. -- this is an interactive program to read in two numbers and print their sum.
  2.  
  3. module Main where
  4.  
  5. main = readInt "Enter first number: " >>= \num1 ->
  6.        readInt "Enter second number: " >>= \ num2 ->
  7.        putStr ("Their sum is " ++ show (num1 + num2) ++ "\n")
  8.  
  9. readInt :: String -> IO Int
  10. readInt prompt =
  11.   putStr prompt >>
  12.   getLine >>= \l -> case (reads l) of
  13.                        [(x,"")] -> return x
  14.                        _        -> putStr "Error - retype the number\n" >>
  15.                                readInt prompt
  16.  
  17.